from openai_client import client
import numpy as np
from numpy import dot
from numpy.linalg import norm


def cos_sim(a, b):
    """余弦距离 -- 越大越相似"""
    return dot(a, b)/(norm(a)*norm(b))


def l2(a, b):
    """欧式距离 -- 越小越相似"""
    x = np.asarray(a)-np.asarray(b)
    return norm(x)


def get_embeddings(texts, model="text-embedding-ada-002"):
    """封装 OpenAI 的 Embedding 模型接口"""
    data = client.embeddings.create(input=texts, model=model).data
    return [x.embedding for x in data]
